from flask import request
from flask import session, jsonify

from info import constants
from info import redis_store
from info.models import User, News, Category
from info.modules import index_blue
from flask import render_template,current_app,g

from info.response_code import RET


@index_blue.route('/newslist')
# @user_login_data
def newslist():
#     1.获取参数
    cid = request.args.get("cid","1")
    page = request.args.get("page","1")
    per_page = request.args.get("per_page","10")

#     2.数据类型转换
    try:
        page = int(page)  # page:哪一页
        per_page = int(per_page) # 每页查询多少条数据
    except Exception as e:
        page = 1
        per_page = 10
#     3.分页查询
    try:

        #判断分类编号是否不等于1
        filter_condition = ""
        if cid != "1":
            filter_condition = (News.category_id == cid)

        paginate = News.query.filter(filter_condition).order_by(News.create_time.desc()).paginate(page,per_page,False)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取新闻失败")
#    4. 获取分页对象属性,总页数,当前页,当前页对象列表
    totalPage = paginate.pages # 总页数
    currentPage = paginate.page # 当前页
    items = paginate.items # 页的列表
#    5. 对象列表转成字典列表
    news_list = []
    for item in items:
        news_list.append(item.to_dict())
    # 6.拼接数据,返回响应

    return jsonify(errno=RET.OK, errmsg="获取成功", totalPage=totalPage, currentPage=currentPage, newsList=news_list)


@index_blue.route('/',methods=["GET","POST"])
def show_index(category_list=None):

    # 1.取出session中用户的编号
    user_id = session.get("user_id")

    # 2.根据user_id取出用户对象
    user = None
    if user_id:
        try:
            user = User.query.get(user_id)
        except Exception as e:
            current_app.logger.error(e)

    # 2.1 查询热门新闻,前10条,按照点击量排序
    try:
        news = News.query.order_by(News.clicks.desc()).limit(constants.CLICK_RANK_MAX_NEWS).all()
        
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询新闻失败")
    # 2.2 将对象列表,转成字典列表
    click_news_list = []
    for item in news:
        click_news_list.append(item.to_dict())

    # 2.3 查询所有分类
    try:
        categories = Category.query.all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取分类失败")
    # 2.4 将匪类对象列表,转成字典列表
    category_list = []
    for category in categories:
        category_list.append(category.to_dict())


    # 3.携带用户数据,渲染页面
    data = {
        # 如果user不为空,返回左侧的值作为结果,否则返回右侧空字符串
        "user_info": user.to_dict() if user else "",
        "click_news_list": click_news_list,
        "categories": category_list
    }
    return render_template("news/index.html", data=data)


@index_blue.route('/favicon.ico')
def get_web_logo():
    return current_app.send_static_file("news/favicon.ico")

